{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd             # data package\n",
    "import matplotlib.pyplot as plt # graphics \n",
    "import datetime as dt\n",
    "import numpy as np\n",
    "\n",
    "import requests, io             # internet and input tools  \n",
    "import zipfile as zf            # zip file tools \n",
    "import os  \n",
    "\n",
    "from numpy.polynomial.polynomial import polyfit\n",
    "\n",
    "import pyarrow as pa\n",
    "import pyarrow.parquet as pq\n",
    "\n",
    "import statsmodels.api as sm\n",
    "import statsmodels\n",
    "#import statsmodels.formula.api as smf\n",
    "from linearmodels.iv import IV2SLS\n",
    "from linearmodels.panel import PanelOLS\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### First Steps\n",
    "\n",
    "Here we are going to combine the trade and autos data set..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig_path = \"C:\\\\github\\\\expenditure_tradeshocks\\\\figures\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "file_path = os.getcwd()\n",
    "\n",
    "trade_county = pq.read_table(file_path + \"\\\\data\\\\trade_employment_blssingle19.parquet\").to_pandas()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "try: driver_flag\n",
    "    \n",
    "except NameError: driver_flag = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "trade_county[\"time\"] = pd.to_datetime(trade_county.time)\n",
    "\n",
    "trade_county.set_index([\"area_fips\", \"time\"],inplace = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "#trade_county.head(40)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "trade_county[\"log_tariff\"] = np.log(1+.01*trade_county[\"tariff\"])\n",
    "\n",
    "trade_county[\"log_exp_total\"] = np.log(trade_county[\"total_exp_pc\"]).replace(-np.inf, np.nan)\n",
    "\n",
    "trade_county[\"log_exp_china\"] = np.log(trade_county[\"china_exp_pc\"]).replace(-np.inf, np.nan)\n",
    "\n",
    "trade_county[\"log_employment\"] = np.log(trade_county[\"emp_gds\"]).replace(-np.inf, np.nan)\n",
    "\n",
    "trade_county[\"const\"] = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "trade_county.reset_index(inplace = True)\n",
    "\n",
    "trade_county.rename({\"area_fips\": \"GEOFIPS\"},axis = 1, inplace = True)\n",
    "\n",
    "trade_county[\"state_fips\"] = trade_county[\"GEOFIPS\"].astype(str).str[0:2]\n",
    "\n",
    "trade_county[\"GEOFIPS\"] = trade_county[\"GEOFIPS\"].astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "#trade_county[\"log_value\"] = np.log(trade_county[\"value\"]).replace(-np.inf,np.nan)\n",
    "\n",
    "trade_county.set_index([\"GEOFIPS\", \"time\"], inplace = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>total_exp_pc</th>\n",
       "      <th>china_exp_pc</th>\n",
       "      <th>tariff</th>\n",
       "      <th>emplvl_2017</th>\n",
       "      <th>fips</th>\n",
       "      <th>total_employment</th>\n",
       "      <th>emp_rtl</th>\n",
       "      <th>emp_all</th>\n",
       "      <th>emp_gds</th>\n",
       "      <th>emp_ngds</th>\n",
       "      <th>rural_share</th>\n",
       "      <th>2010_population</th>\n",
       "      <th>2017_income</th>\n",
       "      <th>2017_population</th>\n",
       "      <th>log_tariff</th>\n",
       "      <th>log_exp_total</th>\n",
       "      <th>log_exp_china</th>\n",
       "      <th>log_employment</th>\n",
       "      <th>const</th>\n",
       "      <th>state_fips</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GEOFIPS</th>\n",
       "      <th>time</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td rowspan=\"5\" valign=\"top\">10001</td>\n",
       "      <td>2016-01-01</td>\n",
       "      <td>453.257185</td>\n",
       "      <td>47.280196</td>\n",
       "      <td>1.069532</td>\n",
       "      <td>2843.0</td>\n",
       "      <td>10001</td>\n",
       "      <td>29514.0</td>\n",
       "      <td>9269.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>38494.0</td>\n",
       "      <td>0.269694</td>\n",
       "      <td>162310.0</td>\n",
       "      <td>57647.0</td>\n",
       "      <td>173145.0</td>\n",
       "      <td>0.010639</td>\n",
       "      <td>6.116460</td>\n",
       "      <td>3.856092</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2016-02-01</td>\n",
       "      <td>471.930726</td>\n",
       "      <td>47.211522</td>\n",
       "      <td>1.069499</td>\n",
       "      <td>2843.0</td>\n",
       "      <td>10001</td>\n",
       "      <td>29514.0</td>\n",
       "      <td>9236.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>38646.0</td>\n",
       "      <td>0.269694</td>\n",
       "      <td>162310.0</td>\n",
       "      <td>57647.0</td>\n",
       "      <td>173145.0</td>\n",
       "      <td>0.010638</td>\n",
       "      <td>6.156832</td>\n",
       "      <td>3.854638</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2016-03-01</td>\n",
       "      <td>485.376760</td>\n",
       "      <td>35.078484</td>\n",
       "      <td>1.069500</td>\n",
       "      <td>2843.0</td>\n",
       "      <td>10001</td>\n",
       "      <td>29514.0</td>\n",
       "      <td>9342.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>38917.0</td>\n",
       "      <td>0.269694</td>\n",
       "      <td>162310.0</td>\n",
       "      <td>57647.0</td>\n",
       "      <td>173145.0</td>\n",
       "      <td>0.010638</td>\n",
       "      <td>6.184925</td>\n",
       "      <td>3.557588</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2016-04-01</td>\n",
       "      <td>460.259354</td>\n",
       "      <td>27.991526</td>\n",
       "      <td>1.069500</td>\n",
       "      <td>2843.0</td>\n",
       "      <td>10001</td>\n",
       "      <td>29514.0</td>\n",
       "      <td>9376.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>39719.0</td>\n",
       "      <td>0.269694</td>\n",
       "      <td>162310.0</td>\n",
       "      <td>57647.0</td>\n",
       "      <td>173145.0</td>\n",
       "      <td>0.010638</td>\n",
       "      <td>6.131790</td>\n",
       "      <td>3.331902</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2016-05-01</td>\n",
       "      <td>473.572638</td>\n",
       "      <td>28.235163</td>\n",
       "      <td>1.069499</td>\n",
       "      <td>2843.0</td>\n",
       "      <td>10001</td>\n",
       "      <td>29514.0</td>\n",
       "      <td>9265.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>40164.0</td>\n",
       "      <td>0.269694</td>\n",
       "      <td>162310.0</td>\n",
       "      <td>57647.0</td>\n",
       "      <td>173145.0</td>\n",
       "      <td>0.010638</td>\n",
       "      <td>6.160305</td>\n",
       "      <td>3.340568</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    total_exp_pc  china_exp_pc    tariff  emplvl_2017   fips  \\\n",
       "GEOFIPS time                                                                   \n",
       "10001   2016-01-01    453.257185     47.280196  1.069532       2843.0  10001   \n",
       "        2016-02-01    471.930726     47.211522  1.069499       2843.0  10001   \n",
       "        2016-03-01    485.376760     35.078484  1.069500       2843.0  10001   \n",
       "        2016-04-01    460.259354     27.991526  1.069500       2843.0  10001   \n",
       "        2016-05-01    473.572638     28.235163  1.069499       2843.0  10001   \n",
       "\n",
       "                    total_employment  emp_rtl  emp_all  emp_gds  emp_ngds  \\\n",
       "GEOFIPS time                                                                \n",
       "10001   2016-01-01           29514.0   9269.0      0.0      0.0   38494.0   \n",
       "        2016-02-01           29514.0   9236.0      0.0      0.0   38646.0   \n",
       "        2016-03-01           29514.0   9342.0      0.0      0.0   38917.0   \n",
       "        2016-04-01           29514.0   9376.0      0.0      0.0   39719.0   \n",
       "        2016-05-01           29514.0   9265.0      0.0      0.0   40164.0   \n",
       "\n",
       "                    rural_share  2010_population  2017_income  \\\n",
       "GEOFIPS time                                                    \n",
       "10001   2016-01-01     0.269694         162310.0      57647.0   \n",
       "        2016-02-01     0.269694         162310.0      57647.0   \n",
       "        2016-03-01     0.269694         162310.0      57647.0   \n",
       "        2016-04-01     0.269694         162310.0      57647.0   \n",
       "        2016-05-01     0.269694         162310.0      57647.0   \n",
       "\n",
       "                    2017_population  log_tariff  log_exp_total  log_exp_china  \\\n",
       "GEOFIPS time                                                                    \n",
       "10001   2016-01-01         173145.0    0.010639       6.116460       3.856092   \n",
       "        2016-02-01         173145.0    0.010638       6.156832       3.854638   \n",
       "        2016-03-01         173145.0    0.010638       6.184925       3.557588   \n",
       "        2016-04-01         173145.0    0.010638       6.131790       3.331902   \n",
       "        2016-05-01         173145.0    0.010638       6.160305       3.340568   \n",
       "\n",
       "                    log_employment  const state_fips  \n",
       "GEOFIPS time                                          \n",
       "10001   2016-01-01             NaN      1         10  \n",
       "        2016-02-01             NaN      1         10  \n",
       "        2016-03-01             NaN      1         10  \n",
       "        2016-04-01             NaN      1         10  \n",
       "        2016-05-01             NaN      1         10  "
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trade_county.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "# First take growth rates\n",
    "\n",
    "# note , I'm a bit confused about why the sorting,\n",
    "# here is that because of some missing values, the resulting dateframe from the \n",
    "# first operation may be out of place, so we need to resort things to make sure that\n",
    "# the time difference is correct.\n",
    "\n",
    "trade_county[\"tariff_change\"] = trade_county.groupby([\"GEOFIPS\"]).tariff.diff(12)\n",
    "\n",
    "trade_county.sort_values([\"GEOFIPS\", \"time\"], inplace = True)\n",
    "\n",
    "trade_county[\"log_tariff_change\"] = trade_county.groupby([\"GEOFIPS\"]).log_tariff.diff(12)\n",
    "\n",
    "trade_county.sort_values([\"GEOFIPS\", \"time\"], inplace = True)\n",
    "\n",
    "trade_county[\"total_trade_growth\"] = trade_county.groupby([\"GEOFIPS\"]).log_exp_total.diff(12)\n",
    "\n",
    "trade_county.sort_values([\"GEOFIPS\", \"time\"], inplace = True)\n",
    "\n",
    "trade_county[\"china_trade_growth\"] = trade_county.groupby([\"GEOFIPS\"]).log_exp_china.diff(12)\n",
    "\n",
    "trade_county.sort_values([\"GEOFIPS\", \"time\"], inplace = True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Regression Analysis\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['total_exp_pc', 'china_exp_pc', 'tariff', 'emplvl_2017', 'fips',\n",
       "       'total_employment', 'emp_rtl', 'emp_all', 'emp_gds', 'emp_ngds',\n",
       "       'rural_share', '2010_population', '2017_income', '2017_population',\n",
       "       'log_tariff', 'log_exp_total', 'log_exp_china', 'log_employment',\n",
       "       'const', 'state_fips', 'tariff_change', 'log_tariff_change',\n",
       "       'total_trade_growth', 'china_trade_growth'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trade_county.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Total Trade, Weighted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "start = \"2018-01-01\"\n",
    "\n",
    "weight_var = '2010_population'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n",
      "3252\n",
      "                          PanelOLS Estimation Summary                           \n",
      "================================================================================\n",
      "Dep. Variable:     total_trade_growth   R-squared:                        0.1076\n",
      "Estimator:                   PanelOLS   R-squared (Between):             -0.0191\n",
      "No. Observations:               51552   R-squared (Within):               0.1964\n",
      "Date:                Thu, Dec 05 2019   R-squared (Overall):              0.1076\n",
      "Time:                        12:27:03   Log-likelihood                 4.972e+04\n",
      "Cov. Estimator:             Clustered                                           \n",
      "                                        F-statistic:                   1.337e+04\n",
      "Entities:                        2864   P-value                           0.0000\n",
      "Avg Obs:                       18.000   Distribution:                 F(1,51550)\n",
      "Min Obs:                       18.000                                           \n",
      "Max Obs:                       18.000   F-statistic (robust):             466.79\n",
      "                                        P-value                           0.0000\n",
      "Time periods:                      18   Distribution:                 F(1,51550)\n",
      "Avg Obs:                       2864.0                                           \n",
      "Min Obs:                       2864.0                                           \n",
      "Max Obs:                       2864.0                                           \n",
      "                                                                                \n",
      "                                 Parameter Estimates                                 \n",
      "=====================================================================================\n",
      "                   Parameter  Std. Err.     T-stat    P-value    Lower CI    Upper CI\n",
      "-------------------------------------------------------------------------------------\n",
      "const                 0.0534     0.0035     15.102     0.0000      0.0465      0.0603\n",
      "log_tariff_change    -4.3626     0.2019    -21.605     0.0000     -4.7584     -3.9668\n",
      "=====================================================================================\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(\"\")\n",
    "print(\"**********************************************************************************\")\n",
    "print(\"Raw Regression Total Trade Growth on Tariff Change\")\n",
    "print(\"\")\n",
    "\n",
    "all_vars = [\"const\", \"log_tariff_change\", 'total_trade_growth','2017_population','2010_population']\n",
    "\n",
    "idx = pd.IndexSlice\n",
    "\n",
    "#weights = trade_county[\"emplvl_2017\"].loc[idx[:,\"2018-01-01\":\"2019-02-01\"]]\n",
    "\n",
    "dataset = trade_county[all_vars].loc[idx[:,start:\"2019-06-01\"],:]\n",
    "\n",
    "exog_vars = [\"const\",\"log_tariff_change\"]\n",
    "\n",
    "weights = dataset[weight_var].to_frame()\n",
    "\n",
    "weights.replace(to_replace = 0, value = 0.001,inplace = True)\n",
    "# Some countys because of there size and how the QCEW deals with stuff, there are is zero.\n",
    "# PanelOLS does not like 0 weight, so this the the work around. It does not matter. \n",
    "\n",
    "\n",
    "mod = PanelOLS(dataset.total_trade_growth, dataset[exog_vars], weights = weights)\n",
    "\n",
    "fe_res = mod.fit(cov_type='clustered', cluster_entity=True)\n",
    "\n",
    "print(fe_res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n",
      "3252\n",
      "                          PanelOLS Estimation Summary                           \n",
      "================================================================================\n",
      "Dep. Variable:     total_trade_growth   R-squared:                        0.0185\n",
      "Estimator:                   PanelOLS   R-squared (Between):              0.0213\n",
      "No. Observations:               51552   R-squared (Within):               0.1027\n",
      "Date:                Thu, Dec 05 2019   R-squared (Overall):              0.0692\n",
      "Time:                        12:27:03   Log-likelihood                 5.699e+04\n",
      "Cov. Estimator:             Clustered                                           \n",
      "                                        F-statistic:                   1.046e+04\n",
      "Entities:                        2864   P-value                           0.0000\n",
      "Avg Obs:                       18.000   Distribution:                 F(1,51533)\n",
      "Min Obs:                       18.000                                           \n",
      "Max Obs:                       18.000   F-statistic (robust):             135.20\n",
      "                                        P-value                           0.0000\n",
      "Time periods:                      18   Distribution:                 F(1,51533)\n",
      "Avg Obs:                       2864.0                                           \n",
      "Min Obs:                       2864.0                                           \n",
      "Max Obs:                       2864.0                                           \n",
      "                                                                                \n",
      "                                 Parameter Estimates                                 \n",
      "=====================================================================================\n",
      "                   Parameter  Std. Err.     T-stat    P-value    Lower CI    Upper CI\n",
      "-------------------------------------------------------------------------------------\n",
      "const                 0.0400     0.0034     11.660     0.0000      0.0333      0.0468\n",
      "log_tariff_change    -1.7556     0.1510    -11.627     0.0000     -2.0516     -1.4597\n",
      "=====================================================================================\n",
      "\n",
      "F-test for Poolability: 988.28\n",
      "P-value: 0.0000\n",
      "Distribution: F(17,51533)\n",
      "\n",
      "Included effects: Time\n"
     ]
    }
   ],
   "source": [
    "print(\"\")\n",
    "print(\"**********************************************************************************\")\n",
    "print(\"Total Trade Growth on Tariff Change, Time Effect\")\n",
    "print(\"\")\n",
    "\n",
    "all_vars = [\"const\", \"log_tariff_change\", 'total_trade_growth','2017_population','2010_population']\n",
    "\n",
    "idx = pd.IndexSlice\n",
    "\n",
    "#weights = trade_county[\"emplvl_2017\"].loc[idx[:,\"2018-01-01\":\"2019-02-01\"]]\n",
    "\n",
    "dataset = trade_county[all_vars].loc[idx[:,start:\"2019-06-01\"],:]\n",
    "\n",
    "exog_vars = [\"const\",\"log_tariff_change\"]\n",
    "\n",
    "weights = dataset[weight_var].to_frame()\n",
    "\n",
    "weights.replace(to_replace = 0, value = 0.001,inplace = True)\n",
    "# Some countys because of there size and how the QCEW deals with stuff, there are is zero.\n",
    "# PanelOLS does not like 0 weight, so this the the work around. It does not matter. \n",
    "\n",
    "\n",
    "mod = PanelOLS(dataset.total_trade_growth, dataset[exog_vars], weights = weights,\n",
    "               time_effects = True)\n",
    "\n",
    "fe_res = mod.fit(cov_type='clustered', cluster_entity=True)\n",
    "\n",
    "print(fe_res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n",
      "3252\n",
      "                          PanelOLS Estimation Summary                           \n",
      "================================================================================\n",
      "Dep. Variable:     total_trade_growth   R-squared:                        0.0144\n",
      "Estimator:                   PanelOLS   R-squared (Between):              0.0210\n",
      "No. Observations:               51552   R-squared (Within):               0.0958\n",
      "Date:                Thu, Dec 05 2019   R-squared (Overall):              0.0650\n",
      "Time:                        12:27:04   Log-likelihood                 8.055e+04\n",
      "Cov. Estimator:             Clustered                                           \n",
      "                                        F-statistic:                   1.037e+04\n",
      "Entities:                        2864   P-value                           0.0000\n",
      "Avg Obs:                       18.000   Distribution:                 F(1,48670)\n",
      "Min Obs:                       18.000                                           \n",
      "Max Obs:                       18.000   F-statistic (robust):             146.47\n",
      "                                        P-value                           0.0000\n",
      "Time periods:                      18   Distribution:                 F(1,48670)\n",
      "Avg Obs:                       2864.0                                           \n",
      "Min Obs:                       2864.0                                           \n",
      "Max Obs:                       2864.0                                           \n",
      "                                                                                \n",
      "                                 Parameter Estimates                                 \n",
      "=====================================================================================\n",
      "                   Parameter  Std. Err.     T-stat    P-value    Lower CI    Upper CI\n",
      "-------------------------------------------------------------------------------------\n",
      "const                 0.0393     0.0007     57.430     0.0000      0.0380      0.0407\n",
      "log_tariff_change    -1.6167     0.1336    -12.102     0.0000     -1.8786     -1.3549\n",
      "=====================================================================================\n",
      "\n",
      "F-test for Poolability: 39.000\n",
      "P-value: 0.0000\n",
      "Distribution: F(2880,48670)\n",
      "\n",
      "Included effects: Entity, Time\n"
     ]
    }
   ],
   "source": [
    "print(\"\")\n",
    "print(\"**********************************************************************************\")\n",
    "print(\"Total Trade Growth on Tariff Change, Time and County Effect\")\n",
    "print(\"\")\n",
    "\n",
    "all_vars = [\"const\", \"log_tariff_change\", 'total_trade_growth','2017_population','2010_population']\n",
    "\n",
    "idx = pd.IndexSlice\n",
    "\n",
    "#weights = trade_county[\"emplvl_2017\"].loc[idx[:,\"2018-01-01\":\"2019-02-01\"]]\n",
    "\n",
    "dataset = trade_county[all_vars].loc[idx[:,start:\"2019-06-01\"],:]\n",
    "\n",
    "exog_vars = [\"const\",\"log_tariff_change\"]\n",
    "\n",
    "weights = dataset[weight_var].to_frame()\n",
    "\n",
    "weights.replace(to_replace = 0, value = 0.001,inplace = True)\n",
    "# Some countys because of there size and how the QCEW deals with stuff, there are is zero.\n",
    "# PanelOLS does not like 0 weight, so this the the work around. It does not matter. \n",
    "\n",
    "\n",
    "mod = PanelOLS(dataset.total_trade_growth, dataset[exog_vars], weights = weights, entity_effects=True,\n",
    "               time_effects = True)\n",
    "\n",
    "fe_res = mod.fit(cov_type='clustered', cluster_entity=True)\n",
    "\n",
    "print(fe_res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Total Trade, unweighted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                          PanelOLS Estimation Summary                           \n",
      "================================================================================\n",
      "Dep. Variable:     total_trade_growth   R-squared:                        0.0473\n",
      "Estimator:                   PanelOLS   R-squared (Between):             -0.0275\n",
      "No. Observations:               53226   R-squared (Within):               0.1007\n",
      "Date:                Thu, Dec 05 2019   R-squared (Overall):              0.0473\n",
      "Time:                        12:27:04   Log-likelihood                 1.784e+04\n",
      "Cov. Estimator:             Clustered                                           \n",
      "                                        F-statistic:                      2644.2\n",
      "Entities:                        2957   P-value                           0.0000\n",
      "Avg Obs:                       18.000   Distribution:                 F(1,53224)\n",
      "Min Obs:                       18.000                                           \n",
      "Max Obs:                       18.000   F-statistic (robust):             184.81\n",
      "                                        P-value                           0.0000\n",
      "Time periods:                      18   Distribution:                 F(1,53224)\n",
      "Avg Obs:                       2957.0                                           \n",
      "Min Obs:                       2957.0                                           \n",
      "Max Obs:                       2957.0                                           \n",
      "                                                                                \n",
      "                                 Parameter Estimates                                 \n",
      "=====================================================================================\n",
      "                   Parameter  Std. Err.     T-stat    P-value    Lower CI    Upper CI\n",
      "-------------------------------------------------------------------------------------\n",
      "const                 0.0251     0.0025     9.8758     0.0000      0.0201      0.0301\n",
      "log_tariff_change    -2.4749     0.1821    -13.595     0.0000     -2.8318     -2.1181\n",
      "=====================================================================================\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "all_vars = [\"const\", \"log_tariff_change\", 'total_trade_growth','2017_population','2010_population']\n",
    "\n",
    "idx = pd.IndexSlice\n",
    "\n",
    "#weights = trade_county[\"emplvl_2017\"].loc[idx[:,\"2018-01-01\":\"2019-02-01\"]]\n",
    "\n",
    "dataset = trade_county[all_vars].loc[idx[:,start:\"2019-06-01\"],:]\n",
    "\n",
    "exog_vars = [\"const\",\"log_tariff_change\"]\n",
    "\n",
    "#weights = dataset['2017_population'].to_frame()\n",
    "\n",
    "#weights.replace(to_replace = 0, value = 0.001,inplace = True)\n",
    "# Some countys because of there size and how the QCEW deals with stuff, there are is zero.\n",
    "# PanelOLS does not like 0 weight, so this the the work around. It does not matter. \n",
    "\n",
    "\n",
    "mod = PanelOLS(dataset.total_trade_growth, dataset[exog_vars])\n",
    "\n",
    "fe_res = mod.fit(cov_type='clustered', cluster_entity=True)\n",
    "\n",
    "if driver_flag != True:\n",
    "\n",
    "    print(fe_res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                          PanelOLS Estimation Summary                           \n",
      "================================================================================\n",
      "Dep. Variable:     total_trade_growth   R-squared:                        0.0029\n",
      "Estimator:                   PanelOLS   R-squared (Between):              0.0042\n",
      "No. Observations:               53226   R-squared (Within):               0.0314\n",
      "Date:                Thu, Dec 05 2019   R-squared (Overall):              0.0200\n",
      "Time:                        12:27:04   Log-likelihood                 2.337e+04\n",
      "Cov. Estimator:             Clustered                                           \n",
      "                                        F-statistic:                      153.35\n",
      "Entities:                        2957   P-value                           0.0000\n",
      "Avg Obs:                       18.000   Distribution:                 F(1,53207)\n",
      "Min Obs:                       18.000                                           \n",
      "Max Obs:                       18.000   F-statistic (robust):             26.584\n",
      "                                        P-value                           0.0000\n",
      "Time periods:                      18   Distribution:                 F(1,53207)\n",
      "Avg Obs:                       2957.0                                           \n",
      "Min Obs:                       2957.0                                           \n",
      "Max Obs:                       2957.0                                           \n",
      "                                                                                \n",
      "                                 Parameter Estimates                                 \n",
      "=====================================================================================\n",
      "                   Parameter  Std. Err.     T-stat    P-value    Lower CI    Upper CI\n",
      "-------------------------------------------------------------------------------------\n",
      "const                 0.0077     0.0026     2.9375     0.0033      0.0026      0.0129\n",
      "log_tariff_change    -0.5957     0.1155    -5.1560     0.0000     -0.8222     -0.3693\n",
      "=====================================================================================\n",
      "\n",
      "F-test for Poolability: 723.95\n",
      "P-value: 0.0000\n",
      "Distribution: F(17,53207)\n",
      "\n",
      "Included effects: Time\n"
     ]
    }
   ],
   "source": [
    "all_vars = [\"const\", \"log_tariff_change\", 'total_trade_growth','2017_population','2010_population']\n",
    "\n",
    "idx = pd.IndexSlice\n",
    "\n",
    "#weights = trade_county[\"emplvl_2017\"].loc[idx[:,\"2018-01-01\":\"2019-02-01\"]]\n",
    "\n",
    "dataset = trade_county[all_vars].loc[idx[:,start:\"2019-06-01\"],:]\n",
    "\n",
    "exog_vars = [\"const\",\"log_tariff_change\"]\n",
    "\n",
    "#weights = dataset['2017_population'].to_frame()\n",
    "\n",
    "#weights.replace(to_replace = 0, value = 0.001,inplace = True)\n",
    "# Some countys because of there size and how the QCEW deals with stuff, there are is zero.\n",
    "# PanelOLS does not like 0 weight, so this the the work around. It does not matter. \n",
    "\n",
    "mod = PanelOLS(dataset.total_trade_growth, dataset[exog_vars], time_effects = True)\n",
    "\n",
    "fe_res = mod.fit(cov_type='clustered', cluster_entity=True)\n",
    "\n",
    "if driver_flag != True:\n",
    "\n",
    "    print(fe_res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                          PanelOLS Estimation Summary                           \n",
      "================================================================================\n",
      "Dep. Variable:     total_trade_growth   R-squared:                        0.0015\n",
      "Estimator:                   PanelOLS   R-squared (Between):              0.0039\n",
      "No. Observations:               53226   R-squared (Within):               0.0257\n",
      "Date:                Thu, Dec 05 2019   R-squared (Overall):              0.0166\n",
      "Time:                        12:27:04   Log-likelihood                  4.38e+04\n",
      "Cov. Estimator:             Clustered                                           \n",
      "                                        F-statistic:                      73.492\n",
      "Entities:                        2957   P-value                           0.0000\n",
      "Avg Obs:                       18.000   Distribution:                 F(1,50251)\n",
      "Min Obs:                       18.000                                           \n",
      "Max Obs:                       18.000   F-statistic (robust):             11.474\n",
      "                                        P-value                           0.0007\n",
      "Time periods:                      18   Distribution:                 F(1,50251)\n",
      "Avg Obs:                       2957.0                                           \n",
      "Min Obs:                       2957.0                                           \n",
      "Max Obs:                       2957.0                                           \n",
      "                                                                                \n",
      "                                 Parameter Estimates                                 \n",
      "=====================================================================================\n",
      "                   Parameter  Std. Err.     T-stat    P-value    Lower CI    Upper CI\n",
      "-------------------------------------------------------------------------------------\n",
      "const                 0.0067     0.0013     5.1019     0.0000      0.0041      0.0093\n",
      "log_tariff_change    -0.4809     0.1420    -3.3874     0.0007     -0.7591     -0.2026\n",
      "=====================================================================================\n",
      "\n",
      "F-test for Poolability: 27.943\n",
      "P-value: 0.0000\n",
      "Distribution: F(2973,50251)\n",
      "\n",
      "Included effects: Entity, Time\n"
     ]
    }
   ],
   "source": [
    "all_vars = [\"const\", \"log_tariff_change\", 'total_trade_growth','2017_population','2010_population']\n",
    "\n",
    "idx = pd.IndexSlice\n",
    "\n",
    "#weights = trade_county[\"emplvl_2017\"].loc[idx[:,\"2018-01-01\":\"2019-02-01\"]]\n",
    "\n",
    "dataset = trade_county[all_vars].loc[idx[:,start:\"2019-06-01\"],:]\n",
    "\n",
    "exog_vars = [\"const\",\"log_tariff_change\"]\n",
    "\n",
    "#weights = dataset['2017_population'].to_frame()\n",
    "\n",
    "#weights.replace(to_replace = 0, value = 0.001,inplace = True)\n",
    "# Some countys because of there size and how the QCEW deals with stuff, there are is zero.\n",
    "# PanelOLS does not like 0 weight, so this the the work around. It does not matter. \n",
    "\n",
    "mod = PanelOLS(dataset.total_trade_growth, dataset[exog_vars], time_effects = True,\n",
    "                              entity_effects=True)\n",
    "\n",
    "fe_res = mod.fit(cov_type='clustered', cluster_entity=True)\n",
    "\n",
    "if driver_flag != True:\n",
    "\n",
    "    print(fe_res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Chine Trade, Weighted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"**********************************************************************************\")\n",
    "print(\"\")\n",
    "print(\"\")\n",
    "print(\"China Results\")\n",
    "print(\"\")\n",
    "print(\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n",
      "3252\n",
      "                          PanelOLS Estimation Summary                           \n",
      "================================================================================\n",
      "Dep. Variable:     china_trade_growth   R-squared:                        0.1692\n",
      "Estimator:                   PanelOLS   R-squared (Between):              0.1691\n",
      "No. Observations:               51552   R-squared (Within):               0.1693\n",
      "Date:                Thu, Dec 05 2019   R-squared (Overall):              0.1692\n",
      "Time:                        12:27:05   Log-likelihood                -1.942e+04\n",
      "Cov. Estimator:             Clustered                                           \n",
      "                                        F-statistic:                      7520.1\n",
      "Entities:                        2864   P-value                           0.0000\n",
      "Avg Obs:                       18.000   Distribution:                 F(1,51550)\n",
      "Min Obs:                       18.000                                           \n",
      "Max Obs:                       18.000   F-statistic (robust):             721.90\n",
      "                                        P-value                           0.0000\n",
      "Time periods:                      18   Distribution:                 F(1,51550)\n",
      "Avg Obs:                       2864.0                                           \n",
      "Min Obs:                       2864.0                                           \n",
      "Max Obs:                       2864.0                                           \n",
      "                                                                                \n",
      "                                 Parameter Estimates                                 \n",
      "=====================================================================================\n",
      "                   Parameter  Std. Err.     T-stat    P-value    Lower CI    Upper CI\n",
      "-------------------------------------------------------------------------------------\n",
      "const                -0.0165     0.0055    -2.9790     0.0029     -0.0274     -0.0056\n",
      "log_tariff_change    -21.678     0.8068    -26.868     0.0000     -23.259     -20.096\n",
      "=====================================================================================\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(\"**********************************************************************************\")\n",
    "print(\"Raw Regrssion China Trade Growth on Tariff Change\")\n",
    "print(\"\")\n",
    "\n",
    "all_vars = [\"const\", \"log_tariff_change\", 'china_trade_growth','2017_population','2010_population']\n",
    "\n",
    "idx = pd.IndexSlice\n",
    "\n",
    "#weights = trade_county[\"emplvl_2017\"].loc[idx[:,\"2018-01-01\":\"2019-02-01\"]]\n",
    "\n",
    "dataset = trade_county[all_vars].loc[idx[:,start:\"2019-06-01\"],:]\n",
    "\n",
    "exog_vars = [\"const\",\"log_tariff_change\"]\n",
    "\n",
    "weights = dataset[weight_var].to_frame()\n",
    "\n",
    "weights.replace(to_replace = 0, value = 0.001,inplace = True)\n",
    "\n",
    "mod = PanelOLS(dataset.china_trade_growth, dataset[exog_vars], weights = weights)\n",
    "\n",
    "fe_res = mod.fit(cov_type='clustered', cluster_entity = True)\n",
    "\n",
    "print(fe_res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n",
      "3252\n",
      "                          PanelOLS Estimation Summary                           \n",
      "================================================================================\n",
      "Dep. Variable:     china_trade_growth   R-squared:                        0.0648\n",
      "Estimator:                   PanelOLS   R-squared (Between):              0.1893\n",
      "No. Observations:               51552   R-squared (Within):               0.1343\n",
      "Date:                Thu, Dec 05 2019   R-squared (Overall):              0.1464\n",
      "Time:                        12:27:05   Log-likelihood                -1.544e+04\n",
      "Cov. Estimator:             Clustered                                           \n",
      "                                        F-statistic:                      91.414\n",
      "Entities:                        2864   P-value                           0.0000\n",
      "Avg Obs:                       18.000   Distribution:                 F(1,51533)\n",
      "Min Obs:                       18.000                                           \n",
      "Max Obs:                       18.000   F-statistic (robust):             275.49\n",
      "                                        P-value                           0.0000\n",
      "Time periods:                      18   Distribution:                 F(1,51533)\n",
      "Avg Obs:                       2864.0                                           \n",
      "Min Obs:                       2864.0                                           \n",
      "Max Obs:                       2864.0                                           \n",
      "                                                                                \n",
      "                                 Parameter Estimates                                 \n",
      "=====================================================================================\n",
      "                   Parameter  Std. Err.     T-stat    P-value    Lower CI    Upper CI\n",
      "-------------------------------------------------------------------------------------\n",
      "const                -0.0574     0.0077    -7.4922     0.0000     -0.0724     -0.0423\n",
      "log_tariff_change    -13.711     0.8260    -16.598     0.0000     -15.330     -12.092\n",
      "=====================================================================================\n",
      "\n",
      "F-test for Poolability: 505.65\n",
      "P-value: 0.0000\n",
      "Distribution: F(17,51533)\n",
      "\n",
      "Included effects: Time\n"
     ]
    }
   ],
   "source": [
    "print(\"\")\n",
    "print(\"**********************************************************************************\")\n",
    "print(\"China Trade Growth on Tariff Change, Time Effect\")\n",
    "print(\"\")\n",
    "\n",
    "all_vars = [\"const\", \"log_tariff_change\", 'china_trade_growth','2017_population','2010_population']\n",
    "\n",
    "idx = pd.IndexSlice\n",
    "\n",
    "#weights = trade_county[\"emplvl_2017\"].loc[idx[:,\"2018-01-01\":\"2019-02-01\"]]\n",
    "\n",
    "dataset = trade_county[all_vars].loc[idx[:,start:\"2019-06-01\"],:]\n",
    "\n",
    "exog_vars = [\"const\",\"log_tariff_change\"]\n",
    "\n",
    "weights = dataset[weight_var].to_frame()\n",
    "\n",
    "weights.replace(to_replace = 0, value = 0.001,inplace = True)\n",
    "\n",
    "mod = PanelOLS(dataset.china_trade_growth, dataset[exog_vars], weights = weights, time_effects = True)\n",
    "\n",
    "fe_res = mod.fit(cov_type='clustered', cluster_entity = True)\n",
    "\n",
    "print(fe_res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n",
      "3252\n",
      "                          PanelOLS Estimation Summary                           \n",
      "================================================================================\n",
      "Dep. Variable:     china_trade_growth   R-squared:                        0.0169\n",
      "Estimator:                   PanelOLS   R-squared (Between):              0.1636\n",
      "No. Observations:               51552   R-squared (Within):               0.1050\n",
      "Date:                Thu, Dec 05 2019   R-squared (Overall):              0.1179\n",
      "Time:                        12:27:06   Log-likelihood                   -7928.4\n",
      "Cov. Estimator:             Clustered                                           \n",
      "                                        F-statistic:                      6111.4\n",
      "Entities:                        2864   P-value                           0.0000\n",
      "Avg Obs:                       18.000   Distribution:                 F(1,48670)\n",
      "Min Obs:                       18.000                                           \n",
      "Max Obs:                       18.000   F-statistic (robust):             119.48\n",
      "                                        P-value                           0.0000\n",
      "Time periods:                      18   Distribution:                 F(1,48670)\n",
      "Avg Obs:                       2864.0                                           \n",
      "Min Obs:                       2864.0                                           \n",
      "Max Obs:                       2864.0                                           \n",
      "                                                                                \n",
      "                                 Parameter Estimates                                 \n",
      "=====================================================================================\n",
      "                   Parameter  Std. Err.     T-stat    P-value    Lower CI    Upper CI\n",
      "-------------------------------------------------------------------------------------\n",
      "const                -0.0777     0.0046    -17.002     0.0000     -0.0867     -0.0687\n",
      "log_tariff_change    -9.7427     0.8913    -10.931     0.0000     -11.490     -7.9957\n",
      "=====================================================================================\n",
      "\n",
      "F-test for Poolability: 9.4939\n",
      "P-value: 0.0000\n",
      "Distribution: F(2880,48670)\n",
      "\n",
      "Included effects: Entity, Time\n"
     ]
    }
   ],
   "source": [
    "print(\"\")\n",
    "print(\"**********************************************************************************\")\n",
    "print(\"China Trade Growth on Tariff Change, Time and County Effect\")\n",
    "print(\"\")\n",
    "\n",
    "all_vars = [\"const\", \"log_tariff_change\", 'china_trade_growth','2017_population','2010_population']\n",
    "\n",
    "idx = pd.IndexSlice\n",
    "\n",
    "#weights = trade_county[\"emplvl_2017\"].loc[idx[:,\"2018-01-01\":\"2019-02-01\"]]\n",
    "\n",
    "dataset = trade_county[all_vars].loc[idx[:,start:\"2019-06-01\"],:]\n",
    "\n",
    "exog_vars = [\"const\",\"log_tariff_change\"]\n",
    "\n",
    "weights = dataset[weight_var].to_frame()\n",
    "\n",
    "weights.replace(to_replace = 0, value = 0.001,inplace = True)\n",
    "\n",
    "mod = PanelOLS(dataset.china_trade_growth, dataset[exog_vars], weights = weights, time_effects = True,\n",
    "               entity_effects=True)\n",
    "\n",
    "fe_res = mod.fit(cov_type='clustered', cluster_entity = True)\n",
    "\n",
    "print(fe_res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### China Trade Unweighted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                          PanelOLS Estimation Summary                           \n",
      "================================================================================\n",
      "Dep. Variable:     china_trade_growth   R-squared:                        0.0974\n",
      "Estimator:                   PanelOLS   R-squared (Between):              0.1165\n",
      "No. Observations:               53226   R-squared (Within):               0.0933\n",
      "Date:                Thu, Dec 05 2019   R-squared (Overall):              0.0974\n",
      "Time:                        12:27:06   Log-likelihood                -5.196e+04\n",
      "Cov. Estimator:             Clustered                                           \n",
      "                                        F-statistic:                      5745.8\n",
      "Entities:                        2957   P-value                           0.0000\n",
      "Avg Obs:                       18.000   Distribution:                 F(1,53224)\n",
      "Min Obs:                       18.000                                           \n",
      "Max Obs:                       18.000   F-statistic (robust):             354.15\n",
      "                                        P-value                           0.0000\n",
      "Time periods:                      18   Distribution:                 F(1,53224)\n",
      "Avg Obs:                       2957.0                                           \n",
      "Min Obs:                       2957.0                                           \n",
      "Max Obs:                       2957.0                                           \n",
      "                                                                                \n",
      "                                 Parameter Estimates                                 \n",
      "=====================================================================================\n",
      "                   Parameter  Std. Err.     T-stat    P-value    Lower CI    Upper CI\n",
      "-------------------------------------------------------------------------------------\n",
      "const                -0.1882     0.0069    -27.116     0.0000     -0.2018     -0.1746\n",
      "log_tariff_change    -13.540     0.7195    -18.819     0.0000     -14.950     -12.130\n",
      "=====================================================================================\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "all_vars = [\"const\", \"log_tariff_change\", 'china_trade_growth','2017_population']\n",
    "\n",
    "idx = pd.IndexSlice\n",
    "\n",
    "#weights = trade_county[\"emplvl_2017\"].loc[idx[:,\"2018-01-01\":\"2019-02-01\"]]\n",
    "\n",
    "dataset = trade_county[all_vars].loc[idx[:,start:\"2019-06-01\"],:]\n",
    "\n",
    "exog_vars = [\"const\",\"log_tariff_change\"]\n",
    "\n",
    "weights = dataset['2017_population'].to_frame()\n",
    "\n",
    "weights.replace(to_replace = 0, value = 0.001,inplace = True)\n",
    "\n",
    "mod = PanelOLS(dataset.china_trade_growth, dataset[exog_vars])\n",
    "\n",
    "fe_res = mod.fit(cov_type='clustered', cluster_entity = True)\n",
    "\n",
    "if driver_flag != True:\n",
    "\n",
    "    print(fe_res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                          PanelOLS Estimation Summary                           \n",
      "================================================================================\n",
      "Dep. Variable:     china_trade_growth   R-squared:                        0.0418\n",
      "Estimator:                   PanelOLS   R-squared (Between):              0.1398\n",
      "No. Observations:               53226   R-squared (Within):               0.0717\n",
      "Date:                Thu, Dec 05 2019   R-squared (Overall):              0.0838\n",
      "Time:                        12:27:06   Log-likelihood                -4.559e+04\n",
      "Cov. Estimator:             Clustered                                           \n",
      "                                        F-statistic:                      2323.8\n",
      "Entities:                        2957   P-value                           0.0000\n",
      "Avg Obs:                       18.000   Distribution:                 F(1,53207)\n",
      "Min Obs:                       18.000                                           \n",
      "Max Obs:                       18.000   F-statistic (robust):             279.09\n",
      "                                        P-value                           0.0000\n",
      "Time periods:                      18   Distribution:                 F(1,53207)\n",
      "Avg Obs:                       2957.0                                           \n",
      "Min Obs:                       2957.0                                           \n",
      "Max Obs:                       2957.0                                           \n",
      "                                                                                \n",
      "                                 Parameter Estimates                                 \n",
      "=====================================================================================\n",
      "                   Parameter  Std. Err.     T-stat    P-value    Lower CI    Upper CI\n",
      "-------------------------------------------------------------------------------------\n",
      "const                -0.2349     0.0065    -36.094     0.0000     -0.2477     -0.2222\n",
      "log_tariff_change    -8.4732     0.5072    -16.706     0.0000     -9.4674     -7.4791\n",
      "=====================================================================================\n",
      "\n",
      "F-test for Poolability: 845.98\n",
      "P-value: 0.0000\n",
      "Distribution: F(17,53207)\n",
      "\n",
      "Included effects: Time\n"
     ]
    }
   ],
   "source": [
    "all_vars = [\"const\", \"log_tariff_change\", 'china_trade_growth','2017_population']\n",
    "\n",
    "idx = pd.IndexSlice\n",
    "\n",
    "#weights = trade_county[\"emplvl_2017\"].loc[idx[:,\"2018-01-01\":\"2019-02-01\"]]\n",
    "\n",
    "dataset = trade_county[all_vars].loc[idx[:,start:\"2019-06-01\"],:]\n",
    "\n",
    "exog_vars = [\"const\",\"log_tariff_change\"]\n",
    "\n",
    "weights = dataset['2017_population'].to_frame()\n",
    "\n",
    "weights.replace(to_replace = 0, value = 0.001,inplace = True)\n",
    "\n",
    "mod = PanelOLS(dataset.china_trade_growth, dataset[exog_vars], time_effects = True)\n",
    "\n",
    "fe_res = mod.fit(cov_type='clustered', cluster_entity = True)\n",
    "\n",
    "if driver_flag != True:\n",
    "\n",
    "    print(fe_res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                          PanelOLS Estimation Summary                           \n",
      "================================================================================\n",
      "Dep. Variable:     china_trade_growth   R-squared:                        0.0121\n",
      "Estimator:                   PanelOLS   R-squared (Between):              0.1282\n",
      "No. Observations:               53226   R-squared (Within):               0.0597\n",
      "Date:                Thu, Dec 05 2019   R-squared (Overall):              0.0719\n",
      "Time:                        12:27:06   Log-likelihood                -3.915e+04\n",
      "Cov. Estimator:             Clustered                                           \n",
      "                                        F-statistic:                      614.09\n",
      "Entities:                        2957   P-value                           0.0000\n",
      "Avg Obs:                       18.000   Distribution:                 F(1,50251)\n",
      "Min Obs:                       18.000                                           \n",
      "Max Obs:                       18.000   F-statistic (robust):             148.83\n",
      "                                        P-value                           0.0000\n",
      "Time periods:                      18   Distribution:                 F(1,50251)\n",
      "Avg Obs:                       2957.0                                           \n",
      "Min Obs:                       2957.0                                           \n",
      "Max Obs:                       2957.0                                           \n",
      "                                                                                \n",
      "                                 Parameter Estimates                                 \n",
      "=====================================================================================\n",
      "                   Parameter  Std. Err.     T-stat    P-value    Lower CI    Upper CI\n",
      "-------------------------------------------------------------------------------------\n",
      "const                -0.2522     0.0050    -50.477     0.0000     -0.2620     -0.2424\n",
      "log_tariff_change    -6.6046     0.5414    -12.200     0.0000     -7.6657     -5.5435\n",
      "=====================================================================================\n",
      "\n",
      "F-test for Poolability: 10.455\n",
      "P-value: 0.0000\n",
      "Distribution: F(2973,50251)\n",
      "\n",
      "Included effects: Entity, Time\n"
     ]
    }
   ],
   "source": [
    "all_vars = [\"const\", \"log_tariff_change\", 'china_trade_growth','2017_population']\n",
    "\n",
    "idx = pd.IndexSlice\n",
    "\n",
    "#weights = trade_county[\"emplvl_2017\"].loc[idx[:,\"2018-01-01\":\"2019-02-01\"]]\n",
    "\n",
    "dataset = trade_county[all_vars].loc[idx[:,start:\"2019-06-01\"],:]\n",
    "\n",
    "exog_vars = [\"const\",\"log_tariff_change\"]\n",
    "\n",
    "weights = dataset['2017_population'].to_frame()\n",
    "\n",
    "weights.replace(to_replace = 0, value = 0.001,inplace = True)\n",
    "\n",
    "mod = PanelOLS(dataset.china_trade_growth, dataset[exog_vars], time_effects = True,\n",
    "               entity_effects=True)\n",
    "\n",
    "fe_res = mod.fit(cov_type='clustered', cluster_entity = True)\n",
    "\n",
    "if driver_flag != True:\n",
    "\n",
    "    print(fe_res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
